<?php

namespace app\index\model;

use think\Model;

// 员工模型
class Employees extends Model
{
    protected $table = "employees";
    protected $pk = "employee_id";
    protected $field = [
        "employee_id",      //员工id
        "first_name",       //第一个名字
        "last_name",        //最后名字
        "department_id",    //部门ids
        "salary",           //工资
    ];


    public function seletEmployeeNoDepartment()
    {
        $employee_list = $this->where("department_id is null")->select();
        // dump($employee_list);die;
        return $employee_list;
    }

    public function selectMaxSalary()
    {
        $employee = $this->order("salary desc")->find();
        return $employee;
    }

    public function selectEmployyeeAndOrder()
    {
        $employee_list = $this->field("first_name,last_name")->order("last_name asc")->select();
        return $employee_list;
    }

    public function selectEmpAndMoreAvgSalary()
    {
        $employee_list = $this->alias("e")->field("e.*")
            ->leftjoin("departments d", "e.department_id = d.department_id")
            ->where("e.salary > (select avg(salary) from employees) and d.department_name like '%Marketing%'")
            ->select();
        return $employee_list;
    }

    public function selectEmpSalaryRange()
    {
        $emp_list = $this->fieldRaw("min(salary),max(salary)")->select();
        return $emp_list;
    }

    public function addEmployeeData($data)
    {
        $add_result = $this->allowField(true)->save($data);
        return $add_result > 0 ? true : false;
    }
}
